package units;

//position @ Array
//Archer // Short // Long // 2-Hand // Spear // Halberdier // Hoplit // Ballista // Siege - Ram // Catapult // Trebuchet // Tower



import java.util.ArrayList;
import java.util.Random;

public class UnitGroup {
	private ArrayList<AbstractUnit> units;
	public boolean silent = false;
	
	
	public UnitGroup() {
		setUnits(new ArrayList<AbstractUnit>());
	}
	
	public void clean() {
		for (int i = 0; i < getUnits().size(); i++) {
			if (!getUnits().get(i).isAlive()) {
				getUnits().remove(i);
				i--;
			}
		}
	}
	
	

	public String toString() {
  	return makeUnitSet().toString();
 	}
	
	
	public void addArchers(int count) {
		for (int i = 0; i < count; i++) {
			getUnits().add(new Archer());
		}
	}
	
	public void addShortSwordsmen(int count) {
		for (int i = 0; i < count; i++) {
			getUnits().add(new ShortSwordsman());
		}
	}
	
	public void addLongSwordsmen(int count) {
		for (int i = 0; i < count; i++) {
			getUnits().add(new LongSwordsman());
		}
	}
	
	public void addTwoHandedSwordman(int count) {
		for (int i = 0; i < count; i++) {
			getUnits().add(new TwoHandedSwordman());
		}
	}
	
	public void addSpearman(int count) {
		for (int i = 0; i < count; i++) {
			getUnits().add(new Spearman());
		}
	}
	
	public void addHalberdier(int count) {
		for (int i = 0; i < count; i++) {
			getUnits().add(new Halberdier());
		}
	}
	
	public void addHoplit(int count) {
		for (int i = 0; i < count; i++) {
			getUnits().add(new Hoplit());
		}
	}
	
	public void addBallista(int count) {
		for (int i = 0; i < count; i++) {
			getUnits().add(new Ballista());
		}
	}
	
	public void addSiegeRam(int count) {
		for (int i = 0; i < count; i++) {
			getUnits().add(new SiegeRam());
		}
	}
	
	public void addCatapult(int count) {
		for (int i = 0; i < count; i++) {
			getUnits().add(new Catapult());
		}
	}
	
	public void addTrebuchet(int count) {
		for (int i = 0; i < count; i++) {
			getUnits().add(new Trebuchet());
		}
	}
	
	public void addTower(int count) {
		for (int i = 0; i < count; i++) {
			getUnits().add(new Tower());
		}
	}
	
	
	
	public int attackRound(UnitGroup enemy, int attackRound) {
		enemy.attackRoundHalf(this, attackRound);
		if (this.getUnits().size() == 0) {
			//TODO: Kampf verloren. Keine Einheiten mehr uebrig.
			return game.Fight.LOOSE;
		}
		this.attackRoundHalf(enemy,  attackRound);
		if (enemy.getUnits().size() == 0) {
			//TODO: Kampf gewonnen. Gegner hat keine Einheiten mehr.
			return game.Fight.WIN;
		}
		return game.Fight.NO_RESULT;
	}
	
	public int defenseRound(UnitGroup enemy, int attackRound) {
		this.attackRoundHalf(enemy, attackRound);
		if (enemy.getUnits().size() == 0) {
			//TODO: Kampf gewonnen. Gegner hat keine Einheiten mehr.
			return game.Fight.WIN;
		}
		enemy.attackRoundHalf(this,  attackRound);
		if (this.getUnits().size() == 0) {
			//TODO: Kampf verloren. Keine Einheiten mehr �brig.
			return game.Fight.LOOSE;
		}
		return game.Fight.NO_RESULT;
	}
	
	
	private void attackRoundHalf(UnitGroup enemy, int attackRound) {
		Random ra = new Random();
		int attackedEnemy = 0;
		for (int i = 0; i < this.getUnits().size(); i++) {
			//Kann ueberhaupt angegriffen werden?!
			if (this.getUnits().get(i).canAttack(attackRound)) {
				
				for (int j = 0; j < this.getUnits().get(i).getAttackMultiple(); j++) {
					if (enemy.getUnits().size() == 0) {return;}
					attackedEnemy = ra.nextInt(enemy.getUnits().size());
					this.getUnits().get(i).attack(enemy.getUnits().get(attackedEnemy));
					if (!enemy.getUnits().get(attackedEnemy).isAlive()) {
						enemy.getUnits().remove(attackedEnemy);
					}
				}
			}
			
		}
	}

	public void setUnits(ArrayList<AbstractUnit> units) {
		this.units = units;
	}

	public ArrayList<AbstractUnit> getUnits() {
		return units;
	}
	
	
	public UnitSet makeUnitSet() {
		int counter[] = {0,0,0,0,0,0,0,0,0,0,0,0};
		for (int i = 0; i< units.size(); i++){
			if (units.get(i) instanceof Archer)
			{   counter[0]++; 
			} else if (units.get(i) instanceof ShortSwordsman) {
				counter[1]++;
			} else if (units.get(i) instanceof LongSwordsman) {
				counter[2]++;
			} else if (units.get(i) instanceof TwoHandedSwordman) {
				counter[3]++;
			} else if (units.get(i) instanceof Spearman) {
				counter[4]++;
			} else if (units.get(i) instanceof Halberdier) {
				counter[5]++;
			} else if (units.get(i) instanceof Hoplit) {
				counter[6]++;
			} else if (units.get(i) instanceof Ballista) {
				counter[7]++;
			} else if (units.get(i) instanceof SiegeRam) {
				counter[8]++;
			} else if (units.get(i) instanceof Catapult) {
				counter[9]++;
			} else if (units.get(i) instanceof Trebuchet) {
				counter[10]++;
			} else if (units.get(i) instanceof Tower) {
				counter[11]++;
		}
		
	}
		return new UnitSet(counter[0],counter[1],counter[2],counter[3],counter[4],counter[5],counter[6],counter[7],counter[8],counter[9],counter[10],counter[11]);
	}
}

